﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.UC.圣同润
{
    class GridControlHelper
    {
        /// <summary>
        /// 禁用选择行样式
        /// </summary>
        /// <param name="view"></param>
        public static void DisableSelectStyle(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;
                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;           //取消焦点框
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;                              //禁用单元格焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = false;                               //禁用整行焦点
            }
        }

        /// <summary>
        /// 启用选择行样式
        /// </summary>
        /// <param name="view"></param>
        public static void EnableSelectStyle(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;
                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFullFocus;   //设置焦点框为整行
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;                              //禁用单元格焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                //启用整行焦点
            }
        }

        /// <summary>
        /// 设置样式
        /// </summary>
        /// <param name="view"></param>
        public static void SetStyles(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;

                gridView.OptionsView.ShowGroupPanel = false;                                              //隐藏最上面的GroupPanel
                gridView.OptionsView.ShowIndicator = false;                                               //隐藏指示列

                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;           //设置焦点框为整行
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;                                  //禁用单元格焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                    //启用整行焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                    //启用整行焦点
                gridView.OptionsSelection.EnableAppearanceHideSelection = false;

                gridView.OptionsView.EnableAppearanceEvenRow = true;                                            //启用偶数行背景色
                gridView.OptionsView.EnableAppearanceOddRow = true;                                             //启用奇数行背景色

                //gridView.Appearance.EvenRow.BackColor = System.Drawing.Color.FromArgb(150, 237, 243, 254);      //设置偶数行背景色
                //gridView.Appearance.OddRow.BackColor = System.Drawing.Color.FromArgb(150, 199, 237, 204);       //设置奇数行背景色
                //gridView.Appearance.FocusedRow.BackColor = System.Drawing.Color.Red;
                //gridView.Appearance.SelectedRow.BackColor = System.Drawing.Color.Red;

            }

            //禁用自动生成列
            view.OptionsBehavior.AutoPopulateColumns = false;
            //禁用自动列宽
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                (view as DevExpress.XtraGrid.Views.Grid.GridView).OptionsView.ColumnAutoWidth = false;
            }
            //禁用数据过滤面板
            view.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;

            #region 添加列

            view.Columns.Clear();

            int index = 0;
            DevExpress.XtraGrid.Columns.GridColumn col = null;

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "NodeText";
            col.Caption = "文件名";
            col.Width = 200;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "CreateDate";
            col.Caption = "创建日期";
            col.Width = 130;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "TaskFile";
            col.Caption = "任务文件";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "FullPath";
            col.Caption = "完整路径";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "Barcode";
            col.Caption = "电池条码";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            #endregion

            SetAllowEdit(view, false);                                          //禁用编辑
            SetAllowSort(view, DevExpress.Utils.DefaultBoolean.False);          //禁用排序
            SetAllowFilter(view, false);                                        //禁用数据过滤
        }

        /// <summary>
        /// 禁用编辑
        /// </summary>
        /// <param name="view"></param>
        /// <param name="isAllow"></param>
        public static void SetAllowEdit(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowEdit = isAllow;
            }
        }
        /// <summary>
        /// 禁用排序
        /// </summary>
        /// <param name="view"></param>
        /// <param name="value"></param>
        public static void SetAllowSort(DevExpress.XtraGrid.Views.Base.ColumnView view, DevExpress.Utils.DefaultBoolean value)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowSort = value;
            }
        }

        /// <summary>
        /// 禁用数据过滤
        /// </summary>
        /// <param name="view"></param>
        /// <param name="isAllow"></param>
        public static void SetAllowFilter(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsFilter.AllowAutoFilter = isAllow;
                col.OptionsFilter.AllowFilter = isAllow;
            }
        }
    }
}
